// var app = angular.module('app', ['ui.router', 'ngAnimate', 'ui.nested.combobox', 'ui.bootstrap', 'molos.tool', 'ngImgCrop', 'w5c.validator']);
var app = angular.module('app', ['ui.router', 'ngAnimate', 'molos.tool', 'ui.bootstrap']);
// app.config(['$stateProvider', '$urlRouterProvider', 'w5cValidatorProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, w5cValidatorProvider, $httpProvider) {
app.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, $httpProvider) {
    $urlRouterProvider.otherwise("/login");
    $stateProvider
        .state('admin', {
            abstract: true,
            templateUrl: 'dashbord'
        })
        .state('admin.demo1', {
            url: '/demo1',
            abstract: true,
            templateUrl: 'demo1'
        })
        .state('admin.demo1.list', {
            url: '/list',
            templateUrl: 'demo1/list'
            // controller: 'ProductListCtrl'
        })
        .state('admin.demo1.publish', {
            url: '/demo1/:pId',
            templateUrl: 'demo1/publish'
            // controller: 'ProductPublishCtrl'
        })
        .state('admin.demo2', {
            url: '/demo2',
            abstract: true,
            templateUrl: 'demo2'
        })
        .state('admin.demo2.delivery', {
            url: '/delivery',
            templateUrl: 'demo2/delivery'
            // controller: 'StockDeliveryCtrl'
        })
        .state('login', {
            url: '/login',
            templateUrl: 'login'
            // controller: 'EmLoginCtrl'
        });

    // 全局配置
    // w5cValidatorProvider.config({
    //     blurTrig: true,
    //     showError: true,
    //     removeError: true
    //
    // });

    $httpProvider.interceptors.push('AuthInterceptor');

}]);
app.run(['$state', '$rootScope', '$log', function ($state, $rootScope, $log) {
    $rootScope.config = {
        initialFrameHeight: 400
    };
    $rootScope.$state = $state;
    $rootScope.pager = {total: 0};
}]);
app.directive('molosPager', [function () {
    var d = {
        scope: {
            mxcall: '&',
            mxcfg: '='
        },
        template: '<uib-pagination first-text="{{mxcfg.first}}" previous-text="{{mxcfg.pre}}" total-items="mxcfg.total" ng-model="mxcfg.cur"  max-size="mxcfg.maxSize" items-per-page="{{mxcfg.size}}" class="pagination-sm" ng-change="changeEvent(mxcfg.cur)" boundary-links="true" force-ellipses="true" next-text="{{mxcfg.next}}" last-text="{{mxcfg.last}}"></uib-pagination>',
        controller: ['$scope', '$element', function ($scope, $element) {
            $scope.changeEvent = function (pageNO) {
                $scope.mxcfg.cur = pageNO;
                if (angular.isFunction($scope.mxcall)) {
                    $scope.mxcall($scope.mxcfg);
                }
            };
            var n = 10;
            var m = 10;
            $scope.mxcfg.cur = $scope.mxcfg.cur || 1;
            $scope.mxcfg.size = $scope.mxcfg.size || n;
            $scope.mxcfg.first = $scope.mxcfg.first || "首页";
            $scope.mxcfg.pre = $scope.mxcfg.pre || "上一页";
            $scope.mxcfg.next = $scope.mxcfg.next || "下一页";
            $scope.mxcfg.last = $scope.mxcfg.last || "末页";
            $scope.mxcfg.total = $scope.mxcfg.total || 0;
            $scope.mxcfg.maxSize = $scope.mxcfg.maxSize || m;
        }],
        restrict: 'E'
    };
    return d;
}]);
app.factory('AuthInterceptor', ['$q', '$injector', function ($q, $injector) {
    var sessionRecoverer = {
        response: function (response) {
            if (response.status === 401) {
                var $state = $injector.get('$state');
                $state.go('login');
                return false;
            }
            return response || $q.when(response);
        },
        responseError: function (response) {
            if (response.status == 401) {
                var $state = $injector.get('$state');
                $state.go('login');
                return false;
            } else {
                return $q.reject(response);
            }
        }
    };
    return sessionRecoverer;
}]);
